package nc.bs.capub.func;

import com.ufida.dataset.DataSetUtilities;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.clusterscheduler.IJobHandler;
import nc.itf.uap.qdi.IQdiDef;
import nc.vo.pub.clusterscheduler.IJob;
import nc.vo.pub.clusterscheduler.ITask;
import nc.vo.pub.qdi.TaskModel;
import nc.vo.pub.qdi.exception.QDIException;
import nc.vo.pub.qdi.scheduler.QDIJob;

public class TestJobHandler implements IJobHandler {
	
	private String dsName;

	public void executeTask(String taskID, String jobID) throws Exception {
		System.out.println("executeTask:"+taskID+","+jobID);
		TaskModel model = null;
		IQdiDef comp = (IQdiDef) NCLocator.getInstance().lookup(IQdiDef.class.getName());
		try {
			model = comp.getTaskModel(taskID, getDsName());
		} catch (QDIException e) {
			Logger.error(e.getMessage(), e);
		}
		if (model != null)
			model.execute();

	}

	public IJob[] getAllTimedJobs() {
		QDIJob[] jobs = null;
		IQdiDef comp = (IQdiDef) NCLocator.getInstance().lookup(IQdiDef.class.getName());
		try {
			jobs = comp.getAllScheduleJob(getDsName());
		} catch (QDIException e) {
			Logger.error(e.getMessage(), e);
		}
		System.out.println("getAllTimedJobs"+jobs.length);
		return jobs;
	}

	public ITask[] splitJob(IJob job) {
		if (job == null || !(job instanceof QDIJob))
			return null;
		ITask[] tasks = null;
		IQdiDef comp = (IQdiDef) NCLocator.getInstance().lookup(IQdiDef.class.getName());
		try {
			tasks = comp.getScheduleTaskArray(job.getJobID(), getDsName());
		} catch (QDIException e) {
			Logger.error(e.getMessage(), e);
		}
		System.out.println("splitJob");
		return tasks;
	}

	private String getDsName() {
		if (dsName == null) {
			dsName = DataSetUtilities.getCurrentDsName();
		}
		System.out.println("getDsName="+dsName);
		return dsName;
	}
	
}
